package com.timzaak.osl.sentry

import org.json4s.JValue
import ws.very.util.json.JsonHelperWithDoubleMode

import scala.util.Try

case class SentryEntity(id: S, projectName: S, culprit: S, level: S, url: S, message: S)

object SentryEntity extends JsonHelperWithDoubleMode {

  import JsonExtractors._

  val Array(jId, jProjectName, jCulprit, jLevel, jUrl, jMessage) = Array(
    "id", "project_name", "culprit", "level", "url", "message").map(param)

  def apply(message: JValue): Try[SentryEntity] = {
    Try {
      message match {
        case jId(str(id)) & jProjectName(str(name)) & jCulprit(str(culprit))
          & jLevel(str(level)) & jUrl(str(url)) & jMessage(str(message)) =>
          SentryEntity(id, name, culprit, level, url, message)
      }
    }
  }
}

object _Test extends App with JsonHelperWithDoubleMode {
  val str =
    """
      |{
      |  "id": "27379932",
      |  "project": "project-slug",
      |  "project_name": "Project Name",
      |  "culprit": "raven.scripts.runner in main",
      |  "level": "error",
      |  "url": "https://app.getsentry.com/getsentry/project-slug/group/27379932/",
      |  "checksum": "c4a4d06bc314205bb3b6bdb612dde7f1",
      |  "logger": "root",
      |  "message": "This is an example Python exception",
      |  "event": {
      |    "extra": {},
      |    "sentry.interfaces.Stacktrace": {
      |      "frames": [
      |        {
      |        }
      |      ]
      |    },
      |    "tags": [
      |      ["foo", "bar"],
      |    ],
      |    "sentry.interfaces.User": {
      |    },
      |    "sentry.interfaces.Http": {
      |    }
      |  }
      |}
    """.stripMargin
  assert(SentryEntity(parseJson(str)).isSuccess, "fail")
}
